package com.two.lbms.xhz.service;

import com.two.lbms.base.BaseService;
import com.two.lbms.dao.BookInfoMapper;
import com.two.lbms.dao.CollectionListMapper;
import com.two.lbms.utils.AssertUtil;
import com.two.lbms.vo.BookInfo;
import com.two.lbms.vo.CollectionList;
import com.two.lbms.xhz.dto.CollectionDTO;
import com.two.lbms.xhz.model.CollectionQuery;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BookCaseService extends BaseService<CollectionList,Integer> {
    @Resource
    private CollectionListMapper collectionListMapper;
    @Resource
    private BookInfoMapper bookInfoMapper;


    /**
     * 添加书本
     * */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addBookInfo(CollectionQuery collectionQuery){

        checkBookInfo(collectionQuery);

        BookInfo bookInfo = bookInfoMapper.selectByPrimaryName(collectionQuery.getBookName());
        AssertUtil.isTrue(bookInfo == null,"书籍并未入库");
        CollectionList collectionList = new CollectionList();
        collectionList.setBookId(bookInfo.getId());
        collectionList.setBookCount(collectionQuery.getBookCount());
        collectionList.setDepositArea(collectionQuery.getDepositArea());
        collectionList.setLocation(collectionQuery.getLocation());
        collectionList.setBookType(collectionQuery.getBookType());
        collectionList.setIsValid(1);

        AssertUtil.isTrue(collectionListMapper.insertSelective(collectionList) < 1,"添加馆藏失败");
    }
    /**
     * 更新
     * */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateBookInfo(CollectionQuery collectionQuery) {
        checkBookInfo(collectionQuery);

        CollectionList collectionList = new CollectionList();

        BookInfo bookInfo = bookInfoMapper.selectByPrimaryName(collectionQuery.getBookName());
        AssertUtil.isTrue(bookInfo == null,"书籍并未入库");
        collectionList.setId(collectionQuery.getId());
        collectionList.setBookId(bookInfo.getId());
        collectionList.setBookCount(collectionQuery.getBookCount());
        collectionList.setDepositArea(collectionQuery.getDepositArea());
        collectionList.setLocation(collectionQuery.getLocation());
        collectionList.setBookType(collectionQuery.getBookType());
        collectionListMapper.updateByPrimaryKeySelective(collectionList);
    }


    private void checkBookInfo(CollectionQuery collectionQuery) {
        AssertUtil.isTrue(StringUtils.isBlank(collectionQuery.getBookName()),"书籍名字不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(collectionQuery.getBookType()),"书籍类型不能为空");
        AssertUtil.isTrue(collectionQuery.getBookCount() == 0,"书籍数量不能为0");
    }
    /**
     * 删除
     * */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBookInfo(Integer[] bookIds){
        collectionListMapper.deleteBatch(bookIds);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public CollectionDTO selectById(Integer id) {

        return collectionListMapper.selectById(id);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBook(Integer id) {
        CollectionList collectionList = new CollectionList();
        collectionList.setId(id);
        collectionList.setIsValid(0);
        AssertUtil.isTrue(collectionListMapper.updateByPrimaryKeySelective(collectionList) < 1,"删除失败");;

    }
}
